/* a_soda.do - ****** ***************************************************************************

This file implements the Student Optimal (proposing) Deferred Acceptance Algorithm to generate
simulated offers. It also evaluates the match replication.

************************************************************************************************/

set more off

local grade "K0 K1"

local year "1997 1998 1999 2000 2001 2002 2003"

mat res = J(17,10,.)
local c = -1

local r = 0
foreach y of local year {
	local r = `r'+1
	local c = -1
	mat res[`r',1] = `y'
foreach g of local grade {
	local c = `c'+3

	use "$stata_data_assignment/`y'/rol`g'.dta", clear


	merge m:1 pid using "$stata_data_assignment/`y'/pcap`g'.dta", gen(_mcap) keep(master matched)

	merge 1:1 pid studentno using "$stata_data_assignment/`y'/pri`g'.dta", gen(_mpri) keep(master matched)

	replace pcap = 0 if missing(pcap)
	drop if mi(pid)

	gen propose = 0
	gen reject = 0

	local remain = 1
	while `remain'>0 {
		sort studentno reject pref, stable
		by studentno reject : replace propose = 1 if _n==1  & reject==0
		sort pid propose prirank rand, stable
		by pid propose: replace reject = 1 if _n>pcap & propose==1
		count if reject==1 & propose==1
		local remain = r(N)
		replace propose = 0 if reject==1
	}


	gen everpropose = reject==1 | propose==1
	sort pid everpropose prirank rand, stable
	by pid everpropose: gen estorder = _n if everpropose==1
	sort pid propose prirank rand, stable
	by pid propose : gen storder = _n if propose==1
	by pid propose : egen maxpri = max(prirank) if propose==1
	by pid propose : egen maxrand = max(rand*(prirank==maxpri)) if propose==1

	save "$stata_data_assignment/`y'/tracks`g'.dta", replace		// this will be used to form the pscore.

	sort studentno propose, stable
	bys studentno: keep if _n==_N

	replace pid = string(studentno) if propose==0

	keep studentno pid propose storder maxpri maxrand prirank rand

	gen unmatched = 1-propose

	preserve
		use "$stata_data_assignment/`y'/StudentIDInfo`g'_modified.dta", clear
		keep studentno pid
		rename pid true
		bys studentno : keep if _n==1
		tempfile assign
		save `assign'
	restore

	merge 1:1 studentno using `assign', gen(_massign) keep(master matched using)

	gen truematch = substr(true,1,7)
	gen ourmatch = substr(pid,1,7)

	gen match = ourmatch==truematch | ((missing(truematch) | missing(truematch)) & unmatched==1)
	count if match==1
	mat res[`r',`c'] = r(N)
	count if match==0
	mat res[`r',`c'+1] = r(N)
	mat res[`r',`c'+2] = res[`r',`c']/(res[`r',`c'] + res[`r',`c'+1])

	sort prirank rand

	keep studentno match
	save "$stata_data_assignment/`y'/correct_match`g'.dta", replace

}
}
* These Match replication rates are featured in Table A2
* The output here can be copied into the tab "A2_mech_replication" in the final Excel deck
putexcel set "$results/raw_tabs/matchrepk0k1", replace
putexcel A1 = matrix(res)
